//https://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc?tpId=182&tqId=34765&ru=/exam/oj

//思路：1.先打印圈；2.打印圈时，逆时针替换；

#include <iostream>
#include <vector>
#include <climits>
#include <stack>
#include <queue>

using namespace std;

class Solution {
public:
    void rotateEdge(vector<vector<int> >& mat,int row1,int col1,int row2,int col2){
        int distance = col2 - col1;
        for (int i = 0;i<distance ;++i){
            int tmp = mat[row1][col1+i];
            mat[row1][col1+i] = mat[row2-i][col1];
            mat[row2-i][col1] = mat[row2][col2-i];
            mat[row2][col2-i] = mat[row1+i][col2];
            mat[row1+i][col2] = tmp;
        }
    }
    vector<vector<int> > rotateMatrix(vector<vector<int> >& mat, int n) {
        // write code here
        if (n<=0||mat.empty()) return {};
        int cur_row = 0;
        int cur_col = 0;
        int rows = mat.size()-1;
        int cols = mat[0].size()-1;
        while (cur_row<=rows) {
            //rotate edge
            rotateEdge(mat,cur_row++,cur_col++,rows--,cols--);
        }
        return mat;
    }
};